import { DEFAULT_FONT_FAMILY, DEFAULT_FONT_SIZE } from "./config";
import { IRect } from "./type";

/**
 * 获取位置，经过缩放处理的值
 * @param e 
 * @returns x,y
 */
export function getXY(e: Event | MouseEvent) {
    const ev = e as MouseEvent;
    return {
        x: ev.offsetX,
        y: ev.offsetY,
    }
}

let _id = 0;
/**
 * 自增ID
 * @returns 
 */
export function UUID() {
    return String(++_id);
}

/**
 * 判断两个矩形是否相交
 * @param rect1 
 * @param rect2 
 * @returns 
 */
export function isRectsIntersect(rect1: IRect, rect2: IRect): boolean {
    // 比较x中点
    const midX1 = rect1.x + rect1.w / 2;
    const midX2 = rect2.x + rect2.w / 2;
    const midXDiff = Math.abs(midX1 - midX2);
    if (midXDiff > Math.abs((rect1.w + rect2.w) / 2)) {
        return false; // x中点差距大于两个矩形宽度之和的一半
    }
    // 比较y中点
    const midY1 = rect1.y + rect1.h / 2;
    const midY2 = rect2.y + rect2.h / 2;
    const midYDiff = Math.abs(midY1 - midY2);
    if (midYDiff > Math.abs((rect1.h + rect2.h) / 2)) {
        return false; // y中点差距大于两个矩形高度之和的一半
    }
    return true; // 两个矩形相交
}

/**
 * 返回字体
 * @param size 
 * @param family 
 * @returns 
 */
export function gFont(size?: number, family?: string) {
    size = size || DEFAULT_FONT_SIZE;
    family = family || DEFAULT_FONT_FAMILY;
    return String(size) + 'px ' + family;
}